#!/usr/bin/env python
# -*- coding: utf-8 -*-
import easyquotation
from statistics import mean
from tool.gongju import *
# from tool.zhibiao import ma_z, ema_z, atr_z
# from tool.MyTT import RSI
from tool.yujingWin import YuyinBobao  # 导入语音播报

quotation = easyquotation.use('tencent')  # 新浪 ['sina'] 腾讯 ['tencent', 'qq']


def 股票拉升预警_v1(cl):
    """
    极速拉升预警，通过买盘的单量远远大于卖盘单量
    """
    # 数据初始化，
    Close = cl.klines[0].close.values
    Datetime = cl.klines[0].datetime.values
    # riqishijian = time_to_datetime(Datetime[-1])
    # shijian = int(riqishijian.time().strftime("%H%M%S"))  # 当前时间,格式为161500
    # riqi = riqishijian.date().strftime("%Y%m%d")  # 当前日期，格式为20210828

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    预警比值 = cl.jiaoyishezhi[5][1]  # 买卖盘比，预警值

    # # 全局变量的初始化，为空的时候执行
    if 'bz' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl.sjb['bz'] = 0
        cl.sjb['相似度'] = 0
        cl.sjb['time'] = time.time()
        # # 指标计算区

    if cl.shangcishijian != Datetime[-1]:  # 更新k线时执行，一般用与计算指标
        ...
        # cl.sjb['bz'] = 0

    if cl.jiaoyiqidong and time.time()-cl.sjb['time'] > 3:
        sygp = quotation.market_snapshot(prefix=True)
        cl.sjb['bz'] = len(sygp.values())
        for k in sygp.values():
            if k['总市值'] and 5 > k['涨跌(%)'] > -3 and k['总市值'] > 20:
                bid_volume = k['bid1_volume']+k['bid2_volume']+k['bid3_volume']+k['bid4_volume']+k['bid5_volume']
                ask_volume = k['ask1_volume']+k['ask2_volume']+k['ask3_volume']+k['ask4_volume']+k['ask5_volume']

                if bid_volume != 0 and ask_volume != 0:
                    pb = round(ask_volume / bid_volume, 2)
                    if k['均价'] > k['now'] and pb > 预警比值:
                        A = np.array([k['ask1_volume'], k['ask2_volume'], k['ask3_volume']])
                        B = np.array([k['ask3_volume'], k['ask4_volume'], k['ask5_volume']])
                        cl.sjb['相似度'] = round(np.dot(A,B)/(np.linalg.norm(A)*np.linalg.norm(B)), 2)
                        if cl.sjb['相似度'] > 0.5:
                            shujuku.publish('ruanjian3.celue', k['code'][2:]+", 将要拉升pb="+str(pb)+" 相似度="+str(cl.sjb['相似度']))
                            YuyinBobao(k['code'] + '拉升')
        cl.sjb['time'] = time.time()

    return cl.sjb['bz'], 0, 0, 0


def 股票拉升预警_v2(cl):
    """
    极速拉升预警，通过n秒涨速判断，涨速达到了设定值预警最快的一只票。
    """
    # 数据初始化，
    Close = cl.klines[0].close.values
    Datetime = cl.klines[0].datetime.values

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    涨速 = cl.jiaoyishezhi[5][1]
    时间 = cl.jiaoyishezhi[5][2]

    # # 全局变量的初始化，为空的时候执行
    if 'bz' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl.sjb['bz'] = 0
        cl.sjb['上次最新价'] = {}
        cl.sjb['time'] = time.time()
        cl.sjb['time2'] = time.time()
        cl.sjb['最大涨速'] = 0
        cl.sjb['涨速列表'] = []
        cl.sjb['预警股票'] = 0
        cl.sjb['涨速'] = 0
        cl.sjb['平均涨速'] = 0
        cl.sjb['上次预警股票'] = '0'

        # 初始化每只股票最新价
        sygp = quotation.market_snapshot(prefix=True)
        for k in sygp.values():
            cl.sjb['上次最新价'][k['code']] = k['now']

    # 指标计算区
    if cl.shangcishijian != Datetime[-1]:  # 更新k线时执行，一般用与计算指标
        ...
        # cl.sjb['bz'] = 0

    if cl.jiaoyiqidong and time.time()-cl.sjb['time'] >= 3:
        cl.sjb['time'] = time.time()
        sygp = quotation.market_snapshot(prefix=True)
        cl.sjb['bz'] = len(sygp.values())
        cl.sjb['涨速列表'] = []
        cl.sjb['最大涨速'] = 0
        cl.sjb['预警股票'] = 0
        for k in sygp.values():
            if time.time() - cl.sjb['time2'] > 时间:
                cl.sjb['上次最新价'][k['code']] = k['now']
                cl.sjb['time2'] = time.time()

            if k['总市值'] and 5 > k['涨跌(%)'] > -8 and k['总市值'] > 20:
                cl.sjb['涨速'] = round((k['now'] - cl.sjb['上次最新价'][k['code']])/k['now']*100, 4)
                cl.sjb['涨速列表'].append(cl.sjb['涨速'])
                if cl.sjb['涨速'] > 涨速:
                    if cl.sjb['涨速'] > cl.sjb['最大涨速']:
                        cl.sjb['最大涨速'] = cl.sjb['涨速']
                        cl.sjb['预警股票'] = k['code'][2:]

        cl.sjb['平均涨速'] = mean(cl.sjb['涨速列表'])
        if cl.sjb['最大涨速'] != 0 and cl.sjb['上次预警股票'] != cl.sjb['预警股票']:
            shujuku.publish('ruanjian3.celue', cl.sjb['预警股票']+", 涨速="+str(cl.sjb['最大涨速']))
            YuyinBobao(cl.sjb['预警股票'] + '拉升')
            cl.sjb['最大涨速'] = 0
            cl.sjb['上次预警股票'] = cl.sjb['预警股票']

    return cl.sjb['bz'], cl.sjb['最大涨速'], cl.sjb['预警股票'], cl.sjb['平均涨速']
